package com.ming.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author:何元才
 * @Desc:
 * @data:2021-03-30 22:53
 */
@RestController
public class ConfigController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${server.port}")
    private Integer port;

    /**
     * gitee  post请求到我这个接口
     * 我自己在发一个请求来刷新
     */
    @PostMapping("myRefresh")
    public ResponseEntity<String> myRefresh() {
        // 组装url
        String url = "http://localhost:" + port + "/actuator/bus-refresh";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("content-type", "application/json;charset=utf-8");
        HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
        // 发请求
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        if (responseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
            return ResponseEntity.ok("刷新成功");
        }
        return ResponseEntity.badRequest().body("刷新失败");
    }

}
